package com.yhc.my.an_meishia.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.yhc.my.an_meishia.R;
import com.yhc.my.an_meishia.utils.DBHelper;

import java.util.HashSet;
import java.util.Set;

public class MedioActivity extends Activity {

    private VideoView vvShow;
    private TextView tvCap;
    private ImageView iv_act;
    private TextView tvShou;
    private String caption;
    private DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_medio);
        final SharedPreferences sharedPreferences = getSharedPreferences("video", MODE_PRIVATE);
        final SharedPreferences.Editor edit = sharedPreferences.edit();
        //初始化控件
        initView();
        //讲bean值传递过来
        Intent intent = getIntent();
        //取值
        final String video = intent.getStringExtra("video");
        //得到文字
        caption = intent.getStringExtra("caption");
        tvCap.setText(caption);
        final Uri uri = Uri.parse(video);
        //播放视频
        vvShow.setVideoURI(uri);
        MediaController controller = new MediaController(MedioActivity.this);
        vvShow.setMediaController(controller);
        vvShow.start();

        tvShou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //得到caption对象
                Set<String> stringSet = sharedPreferences.getStringSet(caption, null);
                Set<String> set = new HashSet<String>();
                if (stringSet!=null){
                    Toast.makeText(MedioActivity.this,"已收藏",Toast.LENGTH_SHORT).show();
                }else {
                    set.add(caption);
                    set.add(video);
                    edit.putStringSet(caption,set);
                    edit.commit();
                    if (edit.commit()){
                        Toast.makeText(MedioActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(MedioActivity.this,"收藏失败",Toast.LENGTH_SHORT).show();
                    }
                }
//                MovieDao dao = new MovieDao(MedioActivity.this);
//                dbHelper = new DBHelper(MedioActivity.this);
//                SQLiteDatabase mDb = dao.getDb();
//                Cursor cursor = mDb.query("meishi", new String[]{"caption"}, null, null, null, null, null, null);
//                SQLiteDatabase writableDatabase = dbHelper.getWritableDatabase();
//                ContentValues values = new ContentValues();
//                //添加到数据库
//                //Log.i("TAG", "----------------- "+cursor.getColumnIndex("caption"));
//                while (cursor.moveToNext()) {
//                    int caption1 = cursor.getColumnIndex("caption");
//                    String string = cursor.getString(caption1);
//                    Log.i("TAG", "-------columnName---------- " + string);
//                    Log.i("TAG", "-------caption---------- "+caption);
//                    if (!string.equals(caption)) {
//                        values.put("caption", caption);
//                        long insert = writableDatabase.insert("meishi", null, values);
//                        if (insert > 0) {
//                            Toast.makeText(MedioActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
//                        }
//                    } else {
//                        Toast.makeText(MedioActivity.this, "已收藏", Toast.LENGTH_SHORT).show();
//                    }
//                }

            }

        });
//        //选择被改变的监听
//        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            private Cursor cursor;
//
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//
//                    // Toast.makeText(MedioActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();
//                    DBHelper helper = new DBHelper(MedioActivity.this);
//                    SQLiteDatabase db = helper.getWritableDatabase();
//                    ContentValues values = new ContentValues();
//                    values.put("cap", caption);
//                        long insert = db.insert("movies", null, values);
//                        Toast.makeText(MedioActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
//                } else {
//                  //  Toast.makeText(MedioActivity.this, "已取消", Toast.LENGTH_SHORT).show();
//                    MovieDao mDb = new MovieDao(MedioActivity.this);
//                    mDb.getDb().delete("movies", "cap=?", new String[]{caption});
//                    Cursor query = mDb.getDb().query("movies", new String[]{"cap"}, null, null, null, null, null);
//                    cursor = query;
//                    Toast.makeText(MedioActivity.this, "取消成功", Toast.LENGTH_SHORT).show();
//                }
//            }
//        });
    }

    private void initView() {
        vvShow = (VideoView) findViewById(R.id.vv_show);
        tvCap = (TextView) findViewById(R.id.tv_cap);
        iv_act = (ImageView) findViewById(R.id.iv_avater);
        tvShou = (TextView) findViewById(R.id.tv_shou);
    }
}
